package com.yibo.user.service.user;

import com.yibo.support.encry.MD5Helper;
import com.yibo.support.helper.Assert;
import com.yibo.support.helper.ObjectHelper;
import com.yibo.support.helper.TimeHelper;
import com.yibo.db.user.entity.LoginError;
import com.yibo.db.user.entity.User;
import com.yibo.db.user.entity.UserInfo;
import com.yibo.db.user.entity.UserLevel;
import com.yibo.db.user.mapper.loginerror.LoginErrorMapper;
import com.yibo.db.user.mapper.user.UserMapper;
import com.yibo.db.user.mapper.userinfo.UserInfoMapper;
import com.yibo.db.user.mapper.userlev.UserLevelMapper;
import com.yibo.user.object.YBUser;

import static com.yibo.support.constant.MsgConstant.*;

/**
 *
 * 通过用户ID 密码登录命令
 *
 * @author zhanglei
 */
public class LoginByPasswordCommand extends BaseLoginCommand {

    /**
     * 用户ID
     */
    private String userId;

    /**
     * 密码
     */
    private String password;


    public LoginByPasswordCommand(String userId, String password){
        initIp();
        this.userId = userId;
        this.password = password;
    }


    @Override
    public YBUser login() {
        //校验
        User user = checkPasswordAndUserId();

        //校验redis信息
        this.result = checkRedisMsg(this.userId);
        if(result != null){
            return result;
        }
        result = new YBUser();

        //查询所有用户信息
        UserInfo info = getBean(UserInfoMapper.class).selectById(this.userId);
        Assert.notEmpty(USER_MESSAGE_QUERY_FAIL,info);

        UserLevelMapper levelMapper = getBean(UserLevelMapper.class);
        UserLevel level = levelMapper.selectById(this.userId);
        Assert.notEmpty(USER_MESSAGE_QUERY_FAIL,level);
        this.result.setUserId(userId)
                .setUserName(user.getUserName())
                .setExtInfo(info)
                .setUserLevel(level)
                .setLastLoginTime(TimeHelper.getNow())
                .setLastQueryTime(TimeHelper.getNow())
                .setIp(this.ip);

        //添加用户信息到redis缓存
        addRedis(this.userId);
        //删除错误登陆信息
        getBean(LoginErrorMapper.class).deleteById(this.ip);
        return result;
    }


    /**
     * 校验用户ID密码
     */
    private User checkPasswordAndUserId(){
        //查询是否登录错误过多
        LoginError error = checkLoginError();
        return checkError(error);
    }


    private  User checkError(LoginError error){
        LoginErrorMapper errorMapper = getBean(LoginErrorMapper.class);
        if(ObjectHelper.hasEmpty(this.userId,this.password)){
            saveLoginError(errorMapper,error,this.userId);
            Assert.notEmpty(USER_ID_IS_NULL,this.userId);
            Assert.notEmpty(PSWD_IS_NULL,this.password);
        }

        User user = getBean(UserMapper.class).selectById(this.userId);
        if(ObjectHelper.isEmpty(user)){
            saveLoginError(errorMapper,error,this.userId);
            Assert.notEmpty(USER_OR_PSWD_ERROR,user);
        }

        String passwd = user.getPassword();
        if(!MD5Helper.getMD5String(this.password).equals(passwd)){
            saveLoginError(errorMapper,error,this.userId);
            Assert.isTrue(USER_OR_PSWD_ERROR,!MD5Helper.getMD5String(this.password).equals(passwd));
        }
        return user;
    }


}
